package com.q.tqb.activity.grab;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.adapter.RedMoreRecordAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.RedRecordInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.MediaHelper;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.CircleImageView;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RedRecordActivity extends BaseActivity {

    @BindView(R.id.iv_back1)
    ImageView mIvBack1;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.civ_head)
    CircleImageView mCivHead;
    @BindView(R.id.rv_record)
    GRecyclerView mRvRecord;

    private ArrayList<RedRecordInfo> mRedRecordInfos;
    private RedMoreRecordAdapter mRedMoreRecordAdapter;

    private int rid;
    private int currentPageNo;


    @Override
    public int getLayoutId() {
        return R.layout.activity_red_record;
    }

    @Override
    public void init(Bundle savedInstanceState) {

        rid = getIntent().getIntExtra("rid", 0);
        String url = getIntent().getStringExtra("headUrl");
        String name = getIntent().getStringExtra("name");

        tvName.setText(name);
        Glide
                .with(RedRecordActivity.this)
                .load(url)
                .error(R.drawable.ic_head)
                .dontAnimate()
                .placeholder(R.drawable.ic_head)
                .into(mCivHead);


        mRedRecordInfos = new ArrayList<>();
        mRvRecord.setLoadingMoreEnabled(true);


        mRvRecord.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPageNo = 1;
                mRvRecord.setNoMore(false);
                mRvRecord.setLoadingMoreEnabled(true);
                getListData();
            }

            @Override
            public void onLoadMore() {
                currentPageNo++;
                getListData();
            }
        });

        mRedMoreRecordAdapter = new RedMoreRecordAdapter(RedRecordActivity.this, mRedRecordInfos, true);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RedRecordActivity.this);
        mRvRecord.setLayoutManager(linearLayoutManager);
        mRvRecord.setAdapter(mRedMoreRecordAdapter);
        mRedMoreRecordAdapter.setOnItemClickListener(new RedMoreRecordAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                String speechAddress = mRedRecordInfos.get(position).getSpeech_address();
                if (!TextUtils.isEmpty(speechAddress)) {
                    MediaHelper.release();
                    MediaHelper.playSound(RedRecordActivity.this, mRedRecordInfos.get(position).getSpeech_address());
                }
            }
        });
        mRvRecord.setRefresh();

    }


    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", currentPageNo + "");
        map.put("pageSize ", 10 + "");
        map.put("rid", rid + "");
        OkHttpUtil.getInstance().post(RedRecordActivity.this, AppConst.URL_GET_PACKET_DETAIL_LIST, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<RedRecordInfo> redUserInfoData = gson.fromJson(element, new TypeToken<ArrayList<RedRecordInfo>>() {
                }.getType());
                if (currentPageNo == 1) {
                    mRedRecordInfos.clear();
                    mRvRecord.refreshComplete();
                } else {
                    if (mRedRecordInfos.size() < 10) {
                        mRvRecord.setNoMore(true);
                    } else {
                        mRvRecord.loadMoreComplete();
                    }
                }
                mRedRecordInfos.addAll(redUserInfoData);
                mRedMoreRecordAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                if (currentPageNo == 1) {
                    mRvRecord.refreshComplete();
                } else {
                    mRvRecord.loadMoreComplete();
                }
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);

            }
        });
    }


    @OnClick(R.id.iv_back1)
    public void onViewClicked() {
        finish();
    }
}
